package com.hh.admin.moduletype.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.moduletype.form.ModuleTypeForm;
import com.hh.admin.moduletype.service.ModuleTypeService;
import com.hh.admin.producttype.form.ProductTypeForm;
import com.hh.admin.producttype.form.ResultProductTypeBean;
import com.hh.common.consts.AdminConst;

/*
 * 模块类型管理action
 */
public class ModuleTypeAction extends MappingDispatchAction{
	private static final Logger log = Logger.getLogger(ModuleTypeAction.class);
	
	private ModuleTypeService moduleService;
    /**
     * 获取模块类型列表
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	ModuleTypeForm moduleTypeForm = (ModuleTypeForm)form;
    	java.util.List<ModuleTypeForm> moduleTypeList = moduleService.getModuleTypeInfoList(moduleTypeForm);
    	request.setAttribute("moduleTypeList", moduleTypeList);
    	//查询总记录数
    	int totleCnt = moduleService.getModuleTypeInfoTotleCnt(moduleTypeForm);
    	moduleTypeForm.setTotleCnt(totleCnt);
    	//计算总页面数
    	int pageCnt = 1;
    	pageCnt = totleCnt / moduleTypeForm.getPageSize();
        if(totleCnt % moduleTypeForm.getPageSize() != 0) {
            pageCnt += 1;
        }
    	moduleTypeForm.setTotlePage(pageCnt);
        return mapping.findForward("list");
    }
    
    /**
     * 删除单模块记录
     * @create 2012-12-26 下午11:47:13
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	ModuleTypeForm moduleTypeForm = (ModuleTypeForm) form;
        
        int id = moduleTypeForm.getId();
        log.debug("模块类型删除，要删除的产品类型ID=[" + id + "]");
        int delCnt = moduleService.deleteOneProducttype(id);
        log.debug("模块类型[" + id + "]删除，影响行数=[" + delCnt + "]");
        return mapping.findForward("list");
    }
    
    /**
     * 添加模块记录
     * @create 2012-12-27 下午8:44:14
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	ModuleTypeForm moduleTypeForm = (ModuleTypeForm) form;
        LoginUserInfo loginUserInfo = (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        moduleTypeForm.setCreate_user(loginUserInfo.getLoginName());
        int newId = moduleService.insertProducttype(moduleTypeForm);
        log.debug("新增产品类型ID=[" + newId + "]");
        return mapping.findForward("success");
    }
    
    /**
     * 打开模块类型详情页进行内容编辑
     */
    public ActionForward detail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	ModuleTypeForm moduleTypeForm = (ModuleTypeForm) form;
        int id = moduleTypeForm.getId();
        ModuleTypeForm result = moduleService.getModuleTypeDetail(id);
        request.setAttribute("moduletypeDetail", result);
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的产品类型内容更新
     * @create 2013-1-1 下午3:59:20
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	ModuleTypeForm moduleTypeForm = (ModuleTypeForm) form;
    	LoginUserInfo loginUserInfo = (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        moduleTypeForm.setCreate_user(loginUserInfo.getLoginName());
        int updCnt = moduleService.updateProductType(moduleTypeForm);
        log.debug("修改产品类型[" + moduleTypeForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        // 更新后刷新框架页面左侧菜单页面
        request.setAttribute("refreshLeft", true);
        return mapping.findForward("success");
    }
    
    /**
     * 批量删除
     * @create 2013-1-1 下午3:27:10
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	ModuleTypeForm moduleTypeForm = (ModuleTypeForm) form;
        Integer[] ids = moduleTypeForm.getIds();
        log.error("需要删除的IDS："+ids.length);
        try {
        	int delCnt = moduleService.deleteManyModuletype(ids);
		} catch (Exception e) {
			log.error(e.toString());
		}
        
        ActionForward forward = mapping.findForward("view");
        String path = forward.getPath();
        log.error("deleteMany path=[" + path + "]");
        return forward;
    }
    
	public ModuleTypeService getModuleService() {
		return moduleService;
	}
	public void setModuleService(ModuleTypeService moduleService) {
		this.moduleService = moduleService;
	}
    
}
